/*
 * Description: 用户状态中心
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/3/10 15:38
 */
import { userQueryBySession, userListAllSimple } from '../../api/sys/user'

const state = {
  // 所有用户简要信息集合（包含名称电话）
  userSimpleCollects: [],
  // 当前会话用户信息
  sessionUser: {
    userId: '',
    postId: '',
    userName: '',
    loginName: '',
    alias: '',
    password: '',
    sex: '',
    email: '',
    mobile: '',
    tel: '',
    createPost: '',
    createUser: '',
    createTime: '',
    modifyPost: '',
    modifyUser: '',
    modifyTime: '',
    recordState: ''
  }
}

const getters = {
  getSessionUser: state => state.sessionUser,
  getUserSimpleCollects: state => state.userSimpleCollects,
  getGcmUserSimpleCollects: state => {
    return state.userSimpleCollects.filter(u => u.team === 'GCM')
  },
  getUserSimpleCollectsByGcmFunction: state => gcmFunction => {
    if (gcmFunction) {
      return state.userSimpleCollects.filter(u => u.gcmFunction === gcmFunction)
    } else {
      return state.userSimpleCollects
    }
  },
  getGcmOwerByGcmFunction: state => gcmFunction => {
    if (gcmFunction) {
      return state.userSimpleCollects.filter(
        u => u.team === 'GCM' && u.gcmFunction === gcmFunction && u.postId.split('_').length < 3
      )
    } else {
      return []
    }
  },
  getEEGcmUserSimpleCollects: state => {
    return state.userSimpleCollects.filter(u => u.team === 'GCM' && u.gcmFunction === 'EE')
  },
  getMEGcmUserSimpleCollects: state => {
    return state.userSimpleCollects.filter(u => u.team === 'GCM' && u.gcmFunction === 'ME')
  },
  getPKGGcmUserSimpleCollects: state => {
    return state.userSimpleCollects.filter(u => u.team === 'GCM' && u.gcmFunction === 'PKG')
  },
  getPbmUserSimpleCollects: state => {
    return state.userSimpleCollects.filter(u => u.team === 'PBM')
  },
  getSubPbmUserSimpleCollects: state => {
    const parentPostId = state.sessionUser.postId + '_'
    return state.userSimpleCollects.filter(
      u => u.team === 'PBM' && u.postId.indexOf(parentPostId) === 0
    )
  },
  getSubPbmUserSimpleCollectsByBu: state => bu => {
    const parentPostId = state.sessionUser.postId + '_'
    return state.userSimpleCollects.filter(u => u.bu === bu && u.postId.indexOf(parentPostId) === 0)
  },
  getUserNameById: state => id => {
    const user = state.userSimpleCollects.find(u => u.userId === id)
    return user ? user.userName : id
  },
  getUserGcmFunctionById: state => id => {
    const user = state.userSimpleCollects.find(u => u.userId === id)
    return user ? user.gcmFunction : ''
  }
}
const mutations = {
  SET_SESSION_USER: (state, sessionUser) => {
    state.sessionUser = sessionUser
  },
  SET_USER_SIMPLE_COLLECTS: (state, userSimpleCollects) => {
    state.userSimpleCollects = userSimpleCollects
  }
}

const actions = {
  initSessionUser({ commit }) {
    return new Promise((resolve, reject) => {
      userQueryBySession()
        .then(response => {
          commit('SET_SESSION_USER', response.data)
          resolve()
        })
        .catch(err => reject(err))
    })
  },
  initUserSimpleCollects({ commit }) {
    return new Promise((resolve, reject) => {
      userListAllSimple()
        .then(response => {
          commit('SET_USER_SIMPLE_COLLECTS', response.data)
          resolve()
        })
        .catch(err => reject(err))
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
